package cn.ycc1.logging.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @Title:
 * @Author ycc
 * @Date 2023/7/1 15:20
 * 可以调整日志级别
 * log.file: D:/logs/logfile.txt
 * log.level.cn.ycc1=debug
 * log.path: D:/logs
 */
//@Slf4j
@RestController
public class HelloController {
    // 记录器
    Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping("/hello")
    public String hello() {
        logger.trace("哈哈，trace日志进来了");
        logger.debug("哈哈，debug日志进来了");

        logger.info("哈哈，info日志进来了");
        logger.warn("哈哈，warn日志进来了");
        logger.error("哈哈，error日志进来了");
        // 不要用system.print.out(), 用日志记录信息
//        log.info("哈哈哈，日志进来了");
        writeLog("=======测试日志写入 logfile.txt ======");
        return "hello logging";
    }

    private static LocalDate today = LocalDate.now();
    private static final String LOG_FILE_PATH = "logs/logfile"+today+".txt";

    public void writeLog(String message) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOG_FILE_PATH, true))) {
            writer.write(message);
            writer.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
